package com.sy.controller;

import com.sy.bean.pucharse.AuditStatus;
import com.sy.bean.pucharse.Condition.ShipmentQueryCondition;
import com.sy.bean.pucharse.Purchase;
import com.sy.bean.pucharse.Shipment;
import com.sy.bean.pucharse.ShipmentQuery;
import com.sy.bean.pucharse.excel.PurchaseExcel;
import com.sy.bean.pucharse.excel.ShipmenteExcel;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.exception.ValidateException;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.ShipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "出货")
public class ShipmentController extends BaseController {
    @Autowired
    ShipmentService shipmentService;

    @ApiOperation("导出出货单列表")
    @PostMapping("/shipment/exportSM")
    public ResultBean<ShipmenteExcel> exportsm(@RequestBody Map map){
        String url=shipmentService.exportsm(map);
        return success("导出出货单列表",url);
    }
    @ApiOperation("出货列表")
    @PostMapping("/shipment/list")
    public ResultBean<ShipmentQuery> purQuery(@RequestBody ShipmentQueryCondition condition) {
        PageBean pageBean = shipmentService.query(BeanOfMap.bom(condition));
        return success("出货列表", pageBean);
    }


    @ApiOperation("出货单详情")
    @GetMapping("/shipment/detail")
    public ResultBean get(int id) {
        Shipment shipment=shipmentService.get(id);
        return success("出货单详情",shipment);
    }

    @ApiOperation("保存出货单")
    @PostMapping("/shipment/save")
    public ResultBean save(@RequestBody Shipment shipment) {
        int rs = shipmentService.save(shipment);
        return gotoResult(rs, "保存");
    }

    @PutMapping("/shipment/update")
    @ApiOperation("修改出货单")
    public ResultBean update(@RequestBody Shipment shipment){
        if (null==shipment.getShipmentDetailList()||shipment.getShipmentDetailList().size()==0)
            throw new ValidateException("shipmentDetailList","采购明细不能为空!");
        shipment.getShipmentDetailList().forEach(o->{
            if (null==o.getGoodsname()||"".equals(o.getGoodsname()))
                throw new ValidateException("Goodsname","商品名称不能为空!");
        });
        int rs = shipmentService.update(shipment);
        return gotoResult(rs,"编辑");
    }
    @ApiOperation("批量删除出货单")
    @DeleteMapping("/shipment/deletes")
    public ResultBean delete(@RequestParam("ids") List<Integer> ids){
        int rs = shipmentService.batchDelete(ids);
        return rs>0?success("删除成功"):fail("删除失败");
    }
    @ApiOperation("批量审核出货单")
    @PutMapping("/shipment/status")
    public ResultBean status(@RequestBody AuditStatus status){
        int rs = shipmentService.batchStatus(status);
        return rs>0?success("审核成功"):fail("审核失败");
    }
}